/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2009 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.pages.game.create;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;

import org.apache.wicket.PageParameters;
import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;

import es.sonxurxo.androidrunner.model.service.game.to.BonusTO;
import es.sonxurxo.androidrunner.model.service.game.to.CoinTO;
import es.sonxurxo.androidrunner.model.service.game.to.CreateGameTO;
import es.sonxurxo.androidrunner.model.service.game.to.GameTO;
import es.sonxurxo.androidrunner.model.service.game.to.ItemTO;
import es.sonxurxo.androidrunner.model.service.game.to.QuestionTO;
import es.sonxurxo.androidrunner.view.web.application.AndroidRunnerApplication;
import es.sonxurxo.androidrunner.view.web.pages.BasePage;

/**
 * @author "Xurxo Mendez Perez"
 *
 */
@AuthorizeInstantiation({"ADMIN", "PLAYER"})
public class CreateGamePage extends BasePage {
	
	public CreateGamePage(PageParameters pageParameters) {
		super();
		
		String city = pageParameters.getString("city");
		int latitude = pageParameters.getInt("latitude");
		int longitude = pageParameters.getInt("longitude");
		int maxPlayers = pageParameters.getInt("maxPlayers");
		int pointsToWin = pageParameters.getInt("pointsToWin");
		int numberOfItems = pageParameters.getInt("numberOfItems");
		
		Set<ItemTO> items = new HashSet<ItemTO>();
		for (int i = 1;i <= numberOfItems; i++) {
			String type = pageParameters.getString("type_" + String.valueOf(i));
			ItemTO itemTO;
			if (type.equals("COI")) {
				itemTO = new CoinTO();
			}
			else if (type.equals("QUE")) {
				QuestionTO questionTO = new QuestionTO();
				itemTO = questionTO;
			}
			else {
				BonusTO bonusTO = new BonusTO();
				bonusTO.setQuestionId(pageParameters.getLong("questionId_" + String.valueOf(i)));
				itemTO = bonusTO;
			}
			itemTO.setId(pageParameters.getLong("itemId_" + String.valueOf(i)));
			itemTO.setLatitude(pageParameters.getInt("itemLatitude_" + String.valueOf(i)));
			itemTO.setLongitude(pageParameters.getInt("itemLongitude_" + String.valueOf(i)));
			itemTO.setPoints(pageParameters.getInt("itemPoints_" + String.valueOf(i)));
			itemTO.setType(type);
			items.add(itemTO);
		}
		
		CreateGameTO createGameTO = new CreateGameTO();
		createGameTO.setCity(city);
		createGameTO.setFinishDate(Calendar.getInstance());
		createGameTO.setStartDate(Calendar.getInstance());
		createGameTO.setLatitude(latitude);
		createGameTO.setLongitude(longitude);
		createGameTO.setMaxPlayers(maxPlayers);
		createGameTO.setPointsToWin(pointsToWin);
		createGameTO.setItems(items);
		
		GameTO gameTO = AndroidRunnerApplication.get().getGameService().createGame(createGameTO);
		
		try {
			this.getResponse().getOutputStream().write(String.valueOf(gameTO.getGameId()).getBytes());
			this.getResponse().getOutputStream().close();
		} catch(IOException e) {
			// TODO
		}
	}

	@Override
	public boolean hasAssociatedMarkup() {
		return false;
	}

	@Override
	protected String getTitle() {
		return null;
	}	
}
